package com.xiaoxiang.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxiang.auth.dao.RoleDao;
import com.xiaoxiang.auth.dao.UserDao;
import com.xiaoxiang.auth.dao.UserRoleDao;
import com.xiaoxiang.auth.entity.Role;
import com.xiaoxiang.auth.entity.User;
import com.xiaoxiang.auth.entity.UserRole;
import com.xiaoxiang.auth.param.UserParam;
import com.xiaoxiang.auth.result.UserResult;
import com.xiaoxiang.auth.service.UserService;
import com.xiaoxiang.common.exception.DefaultException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author haha
 * @since 2022-02-07 18:12:12
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserRoleDao userRoleDao;

    @Resource
    private RoleDao roleDao;

    @Override
    public User queryByUsername(String username) {
        if (username == null) {
            throw new NullPointerException("用户名不能为空");
        }
        User user = User.builder().username(username).isEnabled(true).build();
        List<User> users = baseMapper.queryAll(user);
        return CollectionUtils.isEmpty(users) ? null : users.get(0);
    }

    @Override
    public User queryByPhone(String phone) {
        if (phone == null) {
            throw new NullPointerException("手机号不能为空");
        }
        User user = User.builder().phone(phone).isEnabled(true).build();
        List<User> users = baseMapper.queryAll(user);
        return CollectionUtils.isEmpty(users) ? null : users.get(0);
    }

    @Override
    public List<User> queryAll(User user) {
        return this.baseMapper.queryAll(user);
    }

    @Override
    public Long addUser(UserParam userParam) {
        User user = this.queryByUsername(userParam.getUsername());
        if (user != null) {
            throw new DefaultException("用户名已存在");
        }
        this.baseMapper.insert(userParam);
        UserRole userRole = UserRole.builder().rid(userParam.getRoleId()).uid(userParam.getId()).build();
        userRoleDao.insert(userRole);
        return userParam.getId();
    }

    @Override
    public UserResult getUserResultById(Long id) {
        User user = this.baseMapper.selectById(id);
        if (user == null) {
            return null;
        }
        List<Role> roles = roleDao.queryRoleByUserId(user.getId());
        UserResult userResult = new UserResult();
        BeanUtils.copyProperties(user, userResult);
        userResult.setRoleList(roles);
        return userResult;
    }

    @Override
    public boolean updateUser(UserParam userParam) {
        this.baseMapper.updateById(userParam);
        UserRole userRole = UserRole.builder().uid(userParam.getId()).build();
        int r = userRoleDao.delete(new QueryWrapper<>(userRole));
        userRole = UserRole.builder().rid(userParam.getRoleId()).uid(userParam.getId()).build();
        userRoleDao.insert(userRole);
        return true;
    }
}
